package com.scs.application.modules.dept.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.scs.application.core.mvc.controller.FlowController;
import com.scs.application.core.utils.StringUtils;
import com.scs.application.modules.base.entity.ReagentTeam;
import com.scs.application.modules.base.service.ReagentTeamService;
import com.scs.application.modules.dept.dto.ApplyByPackTempDTO;
import com.scs.application.modules.dept.dto.ApplyItemDTO;
import com.scs.application.modules.dept.entity.Apply;
import com.scs.application.modules.dept.entity.ApplyItem;
import com.scs.application.modules.dept.service.ApplyItemService;
import com.scs.application.modules.dept.service.ApplyService;
import com.scs.application.modules.sys.model.RestResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 */
@RestController
@RequestMapping("/dept/apply")
public class ApplyController extends FlowController<ApplyService, Apply> {
    @Autowired
    private ReagentTeamService reagentTeamService;
    @Autowired
    private ApplyItemService applyItemService;

    /**
     * 查询本周科室请领请购
     * @param
     * @return
     */
    @GetMapping("/findDeptApply")
    public RestResult findDeptApply() {
        List<ApplyItemDTO> deptList = service.findDeptApply();
        return RestResult.ok("查询成功",deptList);
    }

    /**
     * 查询本周科室请领请购单数量
     * @param
     * @return
     */
    @GetMapping("/findThisWeekDeptApplyCount")
    public RestResult findThisWeekDeptApplyCount() {
        Integer count = service.findThisWeekDeptApplyCount();
        return RestResult.ok("查询成功",count);
    }

    @PostMapping("addByTemp")
    public RestResult addByTemp(@RequestBody ApplyByPackTempDTO applyByPackTempDTO) {
        return RestResult.ok("OK", service.addByTemp(applyByPackTempDTO));
    }

    //明细作废
    @PostMapping("closeItem")
    public  RestResult closeItem(@RequestBody Map<String, String> para){
        return RestResult.ok(service.closeItem(para.get("id"),para.get("remark")) );
    }

    /**
     * 根据mscm_code查询耗材
     * @param para
     */
    @PostMapping("/copyApply")
    public RestResult copyApplyAndItems(@RequestBody Map para){
        String id = para.get("id").toString();
        return service.copyApplyAndItems(id);
    }

    private void fillEntity(Apply entity) {
        if (StringUtils.isNotBlank(entity.getReagentTeamId())) {
            ReagentTeam reagentTeam =reagentTeamService.getById(entity.getReagentTeamId());
            if (reagentTeam != null) {
                entity.setReagentTeamName(reagentTeam.getName());
            }
        }

        entity.setCountDtl(applyItemService.count(Wrappers.<ApplyItem>query().select("id").eq("apply_id",entity.getId())));
    }

    /**
     * 根据ID查找数据
     */
    @GetMapping("/find")
    public RestResult find(@RequestParam("id") Serializable id) {
        Apply entity = service.getById(id);
        if (entity == null) {
            return RestResult.error("数据查询失败<br>可能所操作数据已被删除，请刷新后重新尝试操作");
        }
        fillEntity(entity);

        return RestResult.ok("获取数据成功", entity);
    }

    /**
     * 添加/更新数据
     */
    @PostMapping("/save")
    public RestResult saveItem(@Valid @RequestBody Apply entity) {
        boolean isOk = service.saveOrUpdate(entity);
        if (!isOk) {
            return RestResult.error("数据保存失败");
        }
        fillEntity(entity);
        return RestResult.ok("数据保存成功", entity);
    }
}
